<?php
class Controller extends CController{
	/**
	 * @var string 默认模板
	 */
	public $layout = '//layouts/layout';
	
	public $logo = '/images/logo.gif';
	
	public $site_info = null;
	
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu = array();
	
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs = array();

	/**
	 * 封装页面数据
	 * @var unknown_type
	 */
	private $data_list = array();
	
	/**
	 * 分页数据
	 * @var CPagination
	 */
	protected $pager = null;
	
	/**
	 * 指定变量，通过此方法将变量传到页面文件
	 * @param unknown_type $key
	 * @param unknown_type $data
	 */
	protected function assign($key, $data = null){
		if($key !== null){
			if(is_array($key) || is_object($key)){
				//如果是对象或者数组
				foreach($key as $k => $v){
					$this->data_list[$k] = $v;
				}
			}else{
				$this->data_list[$key] = $data;
			}
		}
	}
	
	/**
	 * 重写方法，用于实现数据分散装配
	 * @see CController::render()
	 */
	public function render($view, $data = null, $return = false){
		$this->assign($data);
		return parent::render($view, $this->data_list, $return);
	}
	
	/**
	 * 重写方法，用于实现数据分散装配
	 * @see CController::renderPartial()
	 */
	public function renderPartial($view, $data = null, $return = false, $processOutput = false){
		$this->assign($data);
		return parent::renderPartial($view, $this->data_list, $return, $processOutput);
	}

	/**
	 * ajax方式错误输出
	 * @param string $error_info 错误信息
	 * @param string|array $data 错误数据
	 */
	public function ajax_error($error_info, $data = null){
		//尝试获取当前是否有事务，如果有，则回滚事务
		$transaction = Yii::app()->db->getCurrentTransaction();
		if($transaction !== null){
			@ $transaction->rollback();
		}
		
		ob_clean();
		echo json_encode(array(
			'result'	=> false,
			'info'		=> $error_info,
			'data' 		=> $data
		));
		Yii::app()->end();
		exit;
	}

	/**
	 * json方式，正常输出
	 * @param string $msg 输出信息
	 * @param string|array $data 输出数据
	 */
	public function ajax_info($msg, $data = null){
		ob_clean();
		echo json_encode(array(
			'result' 	=> true,
			'info'		=> $msg,
			'data' 		=> $data
		));
		Yii::app()->end();
		exit;
	}
	
	public function init(){
		if(!defined('BASE_URL')){
			define('BASE_URL', Yii::app()->request->baseUrl);
		}
		$site = SiteModel::getSite();
		Yii::app()->name = $site['name'];
		if(isset($site['pictures'])){
			$this->logo = $site['pictures'][0]['path'];
		}
		if(isset($site['site_info']) && !empty($site['site_info'])){
			$this->site_info = $site['site_info'];
		}
		parent::init();
	}
	
	/**
	 * 获取分页
	 */
	protected function setPagination($total = 0){
		$page_size = DEFAULT_PAGE_SIZE;
		if(isset($_GET['page_size'])){
			$page_size = intval($_GET['page_size']);
			if($page_size <= 0){
				$page_size = DEFAULT_PAGE_SIZE;
			}
		}
		$this->pager = new CPagination($total);
		$this->pager->setPageSize($page_size);
	}
}